package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.ClassCourse;
import com.example.result.Result;
import com.example.service.ClassCourseService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 班级课程控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class ClassCourseController {

    @Autowired
    ClassCourseService classCourseService;

    // ===============================班级课程管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加班级课程信息
     */
    @RequestMapping(value = "/classCourses", method = RequestMethod.POST)
    public Result addClassCourse(@RequestBody String json) {

        return classCourseService.addClassCourse(json);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新班级课程信息
     */
    @RequestMapping(value = "/classCourses", method = RequestMethod.PUT)
    public Result updateClassCourse(@RequestBody String json) {
        // 将JSON数据转换为ClassCourse对象实体
        JSONObject parseObject = JSON.parseObject(json);
        ClassCourse classCourse = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), ClassCourse.class);
        return classCourseService.updateClassCourse(classCourse);

    }

    /**
     * @param classCourseId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据班级课程编号删除班级课程信息
     */
    @RequestMapping(value = "/classCourses/{classCourseId}", method = RequestMethod.DELETE)
    public Result deleteClassCourseById(@PathVariable("classCourseId") String classCourseId) {
        return classCourseService.deleteClassCourseById(classCourseId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有班级课程信息
     */
    @RequestMapping(value = "/classCourses", method = RequestMethod.GET)
    public Result queryAllClassCourse() {
        return classCourseService.queryAllClassCourse();

    }

    /**
     * @param classCourseId
     * @return 以JSON格式返回该班级课程信息
     * @describe 根据编号查询班级课程信息
     */
    @RequestMapping(value = "/classCourses/{classCourseId}", method = RequestMethod.GET)
    public Result queryClassCourseById(@PathVariable("classCourseId") String classCourseId) {
        return classCourseService.queryClassCourseById(classCourseId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加班级课程信息
     */
    @RequestMapping(value = "/classCourses/excels", method = RequestMethod.POST)
    public Result addClassCourseByExcel(@RequestBody String json) {
        JSONObject parseObject = JSON.parseObject(json);
        List<ClassCourse> list = JSON.parseArray(parseObject.getString("json"), ClassCourse.class);

        return classCourseService.addClassCourseByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据班级课程编号批量删除班级课程信息
     */
    @RequestMapping(value = "/classCourses/dels", method = RequestMethod.POST)
    public Result deleteClassCourseByIds(@RequestBody Map<String, String> Ids) {

        return classCourseService.deleteClassCourseByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 班级课程信息模糊查询
     */
    @RequestMapping(value = "/classCourses/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return classCourseService.serachByInfo(info);
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有未分配的班级名称
     */
    @RequestMapping(value = "/classCourses/classNames", method = RequestMethod.GET)
    public Result queryAllClassNameOfNotAllocation() {
        return classCourseService.queryAllClassNameOfNotAllocation();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有课程和班级的已分配信息
     */
    @RequestMapping(value = "/classCourses/classAndCourses", method = RequestMethod.GET)
    public Result queryAllCourseAndClassInfo() {
        return classCourseService.queryAllCourseAndClassInfo();
    }
}
